package com.yang.test.controller;

import com.yang.test.domain.DTO.ResultDTO;
import com.yang.test.domain.Result;
import com.yang.test.domain.VO.ResultMVO;
import com.yang.test.domain.VO.TestQuestionsVO;
import com.yang.test.service.MbtiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.Instant;
import java.util.Map;


// TODO 改为Restful风格
@Slf4j
@RestController
@RequestMapping("/api")
public class MbtiController {
    
    @Autowired
    private MbtiService mbtiService;

    // TODO 请使用aop记录日志
    @GetMapping("/questions")
    public Result<?> getQuestions(@RequestParam(value = "testId", defaultValue = "1") String testId) {
        Instant start = Instant.now();
        log.info("开始获取题目, 测试ID: {}", testId);
        try {
            TestQuestionsVO testQuestions = mbtiService.getTestQuestions(testId);
            
            if (testQuestions == null) {
                return Result.error("未找到相关测试或题目");
            }
            
            log.info("获取题目完成, 测试ID: {}, 题目数量: {}, 总耗时: {}", 
                    testId, testQuestions.getList().size(), Duration.between(start, Instant.now()));
            
            return Result.success(testQuestions);
        } catch (Exception e) {
            log.error("获取题目失败: {}", e.getMessage(), e);
            return Result.error("获取题目失败: " + e.getMessage());
        }
    }

    // TODO 与前端敲定需求再进行开发
    @PostMapping("/submit")
    public Result<?> submitAnswers(@RequestBody ResultDTO resultDTO,
                                   @RequestParam(value = "testId", defaultValue = "1") String testId) {
        Instant start = Instant.now();
        log.info("开始处理答案提交请求");
        
        try {
            String resultId = mbtiService.processAnswers(answers, testId);
            
            log.info("答案提交处理完成, 结果ID: {}, 总耗时: {}", 
                    resultId, Duration.between(start, Instant.now()));
            
            return Result.success(Map.of("resultId", resultId));
        } catch (Exception e) {
            log.error("处理答案提交请求失败: {}", e.getMessage(), e);
            return Result.error("处理请求失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/result/{id}")
    public Result<ResultMVO> getResult(@PathVariable("id") String resultId) {
        Instant start = Instant.now();
        log.info("开始获取结果, ID: {}", resultId);
        
        try {
            ResultMVO result = mbtiService.getResult(resultId);
            
            if (result == null) {
                return Result.error("未找到相关结果");
            }
            
            log.info("获取结果完成, ID: {}, 总耗时: {}", 
                    resultId, Duration.between(start, Instant.now()));
            
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取结果失败: {}", e.getMessage(), e);
            return Result.error("获取结果失败: " + e.getMessage());
        }
    }
}